package tests.utils;

import junit.framework.Assert;
import misc.Function;

import org.junit.Test;

import utils.math.MathCodes;

public class IntegralsCalcusTest {

    final private int PANEL_COUNT = 1000;

    @Test
    public void calcPolynomsIntegralTest() {
	double[] polyCoeff = { 0, 0, 1 };
	Polynom poly = new Polynom(polyCoeff);
	double integral = MathCodes
		.calculateIntegralBR(poly, 0, 3, PANEL_COUNT);
	System.out.println(integral);
	Assert.assertTrue(Math.abs(integral - 9) < 0.0001);

    }

    @Test
    public void calcaSinIntegralTest() {
	Sin sinuns = new Sin();
	double integral = MathCodes.calculateIntegralBR(sinuns, 0, 1,
		PANEL_COUNT);

	System.out.println(integral);
	Assert.assertTrue(Math.abs(integral - (1 - Math.cos(1))) < 0.0001);
    }

    @Test
    public void blindSearchTest() {
	double[] polyCoef = { 0.2, -1.85, 3.2, 5.4, 0, 7.5 };
	Polynom poly = new Polynom(polyCoef);

	double[] answer = MathCodes.blindSearch(poly, 0, 1.6);

	System.out.println("( " + answer[0] + "," + answer[1] + ")");

	MyFinction func = new MyFinction();

	answer = MathCodes.blindSearch(func, -5, 1.6);

	System.out.println("( " + answer[0] + "," + answer[1] + ")");

    }

    private class Polynom extends Function {

	private final double[] poly;

	public Polynom(double[] polyCoeff) {
	    poly = polyCoeff;
	}

	@Override
	public double valueAtPoint(double point) {

	    return MathCodes.horner(poly, point);
	}

    }

    private class Sin extends Function {

	@Override
	public double valueAtPoint(double point) {
	    return Math.sin(point);
	}

    }

    private class MyFinction extends Function {

	@Override
	public double valueAtPoint(double point) {
	    // TODO Auto-generated method stub
	    return 1 + Math.cos(point) - 3 * point;
	}

    }
}
